﻿using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using ZilLion.Service.Core.Log;
using ZilLion.Service.Crm.CommonRepository;
using ZilLion.Service.Crm.ModelBase.EntityBase.Error;
using ResponseResultInfo = ZilLion.Service.Crm.ModelBase.EntityBase.Identity.ResponseResultInfo;

namespace ZilLion.Service.WebInfrastructure.Filter
{
    public class APIExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            ////其他错误通用处理
            if (context.Exception is SpException)
            {
                var exception =
                    context.Exception as SpException;
                ResponseResultInfo error = new ResponseResultInfo();
                //
                LogMongoDb(exception.exeException);
                ErrorMessage msg = new ErrorMessage(exception.exeException, "页面");
                string Message = msg.Message;
                error.Description = Message;
                if (HasErrormodel(exception.errormodel) && exception.Exceptiontype == ExceptionType.Sql)
                {
                    error.Errormodel = exception.errormodel;
                }
                if (exception.Exceptiontype == ExceptionType.Pipe)
                {
                    error.DBCommitted = true;
                    if (HasErrormodel(exception.errormodel))
                    {
                        error.Errormodel = exception.errormodel;
                    }
                }
                //context.HttpContext.Response.wr
                //System.Net.Http
                //(context.HttpContext.Request as HttpRequestMessage).cre
                string logstr = JsonConvert.SerializeObject(error, Formatting.Indented);
                context.HttpContext.Response.ContentType = "text/plain";
                context.HttpContext.Response.WriteAsync(logstr + HttpStatusCode.ServiceUnavailable);
                //HttpContextMessage
                //context.HttpContext.Request..
                //context.HttpContext.Response =
                //     context.HttpContext.Request.CreateResponse(HttpStatusCode.ServiceUnavailable, error);
            }
            //未知情况
            else
            {

                ErrorMessage msg = new ErrorMessage(context.Exception, "页面");
                LogMongoDb(context.Exception);
                ResponseResultInfo error = new ResponseResultInfo();
                string Message = msg.Message+"Stack:"+ msg.StackTrace;
                error.Description = Message;
                string logstr = JsonConvert.SerializeObject(error, Formatting.Indented);
                context.HttpContext.Response.ContentType = "text/plain";
                context.HttpContext.Response.WriteAsync(logstr + HttpStatusCode.ServiceUnavailable);
                //context.Response =
                //   context.Request.CreateResponse(HttpStatusCode.ServiceUnavailable, error);
            }

        }

        private void LogMongoDb(System.Exception msgex)
        {
            try
            {
                ZilLionLogManager.Instance().Error(msgex,tags: "页面");
            }
            catch (System.Exception ex)
            {

            }
        }

        private bool HasErrormodel(ErrorModel errormodel)
        {
            if (errormodel == null)
            {
                return false;
            }
            if (string.IsNullOrEmpty(errormodel.Errtype))
            {
                return false;
            }
            return true;
        }
    }
}
